{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 2.1 会话间模型调用的消息列表\n",
    "2.1.1：使用对话模型的消息列表"
   ],
   "id": "78e1ead53aaeb2e3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T23:03:35.291444Z",
     "start_time": "2025-10-20T23:03:34.793507Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain_core.messages import HumanMessage,SystemMessage\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "import os\n",
    "import dotenv\n",
    "dotenv.load_dotenv()\n",
    "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"DASHSCOPE_API_KEY\")\n",
    "os.environ[\"OPENAI_BASE_URL\"] = os.getenv(\"DASHSCOPE_BASE_URL\")\n",
    "messages = [\n",
    "    SystemMessage(content=\"You are a helpful assistant that translates English to Chinese.\"),\n",
    "    HumanMessage(content=\"I love programming.\"),\n",
    "]\n",
    "chat = ChatOpenAI(\n",
    "    model=\"qwen-max\",\n",
    "    temperature=0.6,\n",
    ")\n",
    "response = chat.invoke(messages)\n",
    "print(messages)\n",
    "# print('\\n')\n",
    "print(response. content)"
   ],
   "id": "9ad4c7c4a8dd6e9d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[SystemMessage(content='You are a helpful assistant that translates English to Chinese.', additional_kwargs={}, response_metadata={}), HumanMessage(content='I love programming.', additional_kwargs={}, response_metadata={})]\n",
      "我爱编程。\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "2.2.2 完整的会话消息列表",
   "id": "5f4e79c2a89f5243"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T23:47:30.614597Z",
     "start_time": "2025-10-20T23:47:26.585356Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain_core.messages import SystemMessage,HumanMessage,AIMessage\n",
    "import dotenv\n",
    "dotenv.load_dotenv()\n",
    "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"DASHSCOPE_API_KEY\")\n",
    "os.environ[\"OPENAI_BASE_URL\"] = os.getenv(\"DASHSCOPE_BASE_URL\")\n",
    "chat_model = ChatOpenAI(\n",
    "    model=\"qwen-max\",\n",
    "    temperature=0.7,\n",
    "    # max_tokens=256,\n",
    ")\n",
    "messages = [\n",
    "    SystemMessage(content=\"你是一位数学家，只会做数学题，每次回答都会给出详细过程.\"),\n",
    "    HumanMessage(content=\"I love programming.\"),\n",
    "    AIMessage(content=\"1+2*3=7。\"),\n",
    "    HumanMessage(content=\"1+2*3=？\"),\n",
    "]\n",
    "print(messages)\n",
    "response = chat_model.invoke(messages)\n",
    "print(response.content)"
   ],
   "id": "4b60013471231637",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[SystemMessage(content='你是一位数学家，只会做数学题，每次回答都会给出详细过程.', additional_kwargs={}, response_metadata={}), HumanMessage(content='I love programming.', additional_kwargs={}, response_metadata={}), AIMessage(content='1+2*3=7。', additional_kwargs={}, response_metadata={}), HumanMessage(content='1+2*3=？', additional_kwargs={}, response_metadata={})]\n",
      "根据数学中的运算顺序规则，即先乘除后加减，我们首先执行乘法运算，然后进行加法运算。所以对于表达式 1 + 2 * 3：\n",
      "\n",
      "- 首先计算 2 * 3 = 6\n",
      "- 然后将结果加上 1，得到 1 + 6 = 7\n",
      "\n",
      "因此，1 + 2 * 3 的结果是 7。\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "2.2.3 创建不同类型消息",
   "id": "ecfe753317db7b75"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-21T00:07:20.189650Z",
     "start_time": "2025-10-21T00:07:20.168580Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from langchain_core.messages import ChatMessage\n",
    "# 1.导入包\n",
    "from langchain_core.messages import SystemMessage,HumanMessage,AIMessage\n",
    "# 2.直接创建不同类型的消息\n",
    "system_message=SystemMessage(\n",
    "    content=\"你是一名AI开发工程师\",\n",
    "    additional_kwargs={\n",
    "        \"tool\": \"invoke_tool()\",\n",
    "        \"model\": \"qwen-max\"\n",
    "}\n",
    ")\n",
    "human_message=HumanMessage(content=\"You love programming,你能开发哪些AI应用？.\")\n",
    "ai_message=AIMessage(content=\"我能开发很多AI应用，比如：1. 聊天机器人，2.企业知识库，3.图像识别，自然语言处理等.\")\n",
    "custom_message = ChatMessage(role=\"analyst\", content=\"补充一点关于超参数调优的信息\")\n",
    "# 3.打印出消息列表\n",
    "messages=[system_message,human_message,ai_message]\n",
    "# print(messages)\n",
    "print(ai_message.content)\n",
    "print(custom_message.content)"
   ],
   "id": "a3c30b1ee4a9e66f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我能开发很多AI应用，比如：1. 聊天机器人，2.企业知识库，3.图像识别，自然语言处理等.\n",
      "补充一点关于超参数调优的信息\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "2.2.5：结合大模型使用",
   "id": "8d3f3b8b0c48c95"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-21T00:09:59.137974Z",
     "start_time": "2025-10-21T00:09:42.124637Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import os\n",
    "from langchain_core.messages import SystemMessage,HumanMessage\n",
    "dotenv.load_dotenv()\n",
    "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"DASHSCOPE_API_KEY\")\n",
    "os.environ[\"OPENAI_BASE_URL\"] = os.getenv(\"DASHSCOPE_BASE_URL\")\n",
    "chat_model = ChatOpenAI(\n",
    "    model=\"qwen-max\",\n",
    "    temperature=0.7,\n",
    ")\n",
    "# 组成消息列表\n",
    "messages = [\n",
    "SystemMessage(content=\"你是一个擅长人工智能相关学科的专家\"),\n",
    "HumanMessage(content=\"请解释一下什么是机器学习？\")\n",
    "]\n",
    "response = chat_model.invoke(messages)\n",
    "print(response.content)\n",
    "print(type(response)) #<class 'langchain_core.messages.ai.AIMessage'>"
   ],
   "id": "163189f23ad386f1",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "机器学习是一种人工智能（AI）技术，它使计算机能够在没有明确编程的情况下从数据中学习并改进其性能。简单来说，机器学习是让计算机通过经验自动学习和改进的过程。\n",
      "\n",
      "在传统的编程方法中，程序员需要编写详细的指令来告诉计算机如何执行特定任务。而在机器学习中，我们提供给计算机大量的数据，并使用算法让计算机自己“学习”出规律或模式，从而能够对新的、未见过的数据做出预测或决策。这种学习过程通常涉及训练一个模型，该模型可以是分类器、回归器或其他类型的预测模型。\n",
      "\n",
      "机器学习可以分为几大类：\n",
      "\n",
      "1. **监督学习**：在这种类型的学习中，算法通过已知输入与输出之间的关系进行训练。目的是让算法能够根据这些例子学会如何将未来的输入映射到正确的输出。常见的应用包括图像识别、语音识别等。\n",
      "   \n",
      "2. **无监督学习**：当数据没有标签时使用这种方法。算法尝试发现数据中的结构或模式，如聚类分析，用来将相似的数据点分组在一起。\n",
      "   \n",
      "3. **强化学习**：这是一种通过奖励机制来进行学习的方法。智能体(agent)会基于环境的状态采取行动，并根据所采取的动作获得奖励或惩罚。目标是最大化累积奖励。这在游戏玩法优化等领域非常有用。\n",
      "\n",
      "随着大数据时代的到来以及计算能力的显著提升，机器学习已经成为解决复杂问题的强大工具，在许多领域都有着广泛的应用，比如自然语言处理、医疗健康、金融风控等等。\n",
      "<class 'langchain_core.messages.ai.AIMessage'>\n"
     ]
    }
   ],
   "execution_count": 31
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
